package com.pg.lockly.key;

import android.content.Context;
import android.util.Log;

/**
 * 获取aes等相关的KEY
 */
public class KeyManager {

    private static final KeyManager ourInstance = new KeyManager();

    public static KeyManager getInstance() {
        return ourInstance;
    }

    private KeyManager() {
    }

    static {
        System.loadLibrary("key");
    }

    protected Context mContext;

    public String getAesKey(){
        String aesKey = a(mContext);
//        if (aesKey!= null && aesKey.equalsIgnoreCase("error")){
//            AnalyticsManager.getIns().onEvent(AnalyticsConfig.ID_VERIFICATION_FAIL,
//                    AnalyticsConfig.KEY_METHOD_NAME,"1");
//        }
        return aesKey;
    }



    public String getLogKey(){
        String logK = b(mContext);
//        if (logK!= null && logK.equalsIgnoreCase("error")){
//            AnalyticsManager.getIns().onEvent(AnalyticsConfig.ID_VERIFICATION_FAIL,
//                    AnalyticsConfig.KEY_METHOD_NAME,"4");
//        }
        return logK;
    }

    /**
     * rsa公钥加密
     * 内部流程：rsa解密后 再Base64解密
     */
    public String encryptRsaPublic(String input){
        return c(mContext,input);
    }

    /**
     * rsa私钥 解密
     * 内部流程：先Base64解密 再rsa解密
     */
    public String decryptRsaPrivate(String input){
        return d(mContext,input);
    }

    /**
     * BASE64 + AES/ECB/PKCS5Padding 解密
     */
    public String decryptAesEcb(String de){
        try {
        Log.i("fredZhu","待解密数据:"+de+"\n length:"+de.length());
            return f(mContext, de);
        }catch (Exception e){
            Log.i("fredZhu","错误信息:"+e.getMessage());
            return "";
        }
    }

    /**
     * AES/ECB/PKCS5Padding + BASE64 加密
     */
    public String encryptAesEcb(String en){
        try {
            return e(mContext, en);
        }catch (Exception e){
            Log.i("fredZhu","错误信息:"+e.getMessage());
            return "";
        }
    }


    /*
     * native 都使用a b c ... 命名 防止根据名字猜功能
     */
    private native String a(Context context);

    private native String b(Context context);

    private native String c(Context context,String input);

    private native String d(Context context,String input);

    private native String e(Context context,String en) ;

    private native String f(Context context,String de);

}
